<!doctype html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>月报数据合并周</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
        <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    </head>
    <body>
        <div class="easyui-container-fluid">
            <div class="easyui-panel" style="border: none;">
                <div class="nav" id="nav"></div>
                <div class="easyui-panel" style="border: none;">
                    <form id="ff">
                        <div style="margin-bottom:20px">

                            <label class="item-label">报告年份：</label>
                            <input type="text" id="year" name="year" class="easyui-combobox" style="width: 100px;"/>
                            <span id="monthLabel">
	                        <label class="item-label">报告月份：</label>
	                        <input id="month" name="month" type="text" class="easyui-combobox" style="width: 100px;"/>
                        </span>
                            <span id="weekLabel">
                            <label class="item-label">报告周：</label>
                        	<input type="text" id="week" name="reportWeek" class="easyui-combobox" style="width: 100px;"/>
                        </span>
                            <label class="item-label">渠道：</label>
                            <input type="text" id="channel" name="channelCode" class="easyui-combobox" style="width: 100px;"/>
                            <label class="item-label">渠道日期：</label>
                            <select id="channelDate" class="easyui-combobox" name="channelDate" style="width:100px;" data-options="panelHeight:'auto'">
                                <option value="">全部</option>
                                <option value="true">是</option>
                                <option value="false">否</option>
                            </select>
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left:40px;" onclick="searchBy()">查询</a>
                            &nbsp;&nbsp;&nbsp;
<!--                            <label class="item-label">合并周范围：</label>-->
<!--                            <input type="text" class="easyui-numberbox" id="startWeek" style="width: 40px" prompt="始"/>/-->
<!--                            <input type="text" class="easyui-numberbox" id="startPercentage" style="width: 40px" prompt="占比"/>% &#45;&#45;-->
<!--                            <input type="text" class="easyui-numberbox" id="endWeek" style="width: 40px" prompt="止"/>-->
<!--                            <input type="text" class="easyui-numberbox" id="endPercentage" style="width: 40px" prompt="占比"/>%-->
                            <a href="javascript:void(0);" class="easyui-linkbutton c6" style="width:80px;margin-left:40px;" onclick="mergeWeek()">开始合并</a>
                        </div>
                    </form>
                </div>
                <div class="table-wrapper">
                    <table id="datagrid" style="width:100%" data-options="fitColumns:true,scrollbarSize:0,striped:true">
                        <thead>
                            <tr>
                                <th field="ck" checkbox="true"></th>
                                <th data-options="field:'code',align:'center'" width="60">渠道码</th>
                                <th data-options="field:'name',align:'center'" width="60">渠道</th>
                                <th data-options="field:'dataReportCode',align:'center'" width="80">报告码</th>
                                <th data-options="field:'reportYear',align:'center'," width="30">年</th>
                                <th data-options="field:'reportMonth',align:'center'," width="30">月份</th>
                                <th data-options="field:'channelDate',align:'center',formatter:dateAction" width="30">渠道日期</th>
                                <th data-options="field:'week1',align:'center',formatter:weekAction" width="30">周1(条)</th>
                                <th data-options="field:'week2',align:'center',formatter:weekAction" width="30">周2(条)</th>
                                <th data-options="field:'week3',align:'center',formatter:weekAction" width="30">周3(条)</th>
                                <th data-options="field:'week4',align:'center',formatter:weekAction" width="30">周4(条)</th>
                                <th data-options="field:'week5',align:'center',formatter:weekAction" width="30">周5(条)</th>
                                <th data-options="field:'week6',align:'center',formatter:weekAction" width="30">周6(条)</th>
                                <th data-options="field:'mergeStartTime',align:'center'" width="80">数据开始时间</th>
                                <th data-options="field:'mergeEndTime',align:'center'" width="80">数据结束时间</th>
                                <th data-options="field:'mergeType',align:'center',formatter:mergeType" width="50">合并状态</th>
                                <th data-options="field:'mergeCount',align:'center'" width="40">记录数</th>
                                <th data-options="field:'action',formatter:actions,align:'center'" width="60">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        $(function () {
            setNav();
            initMonthWeek("monthLabel", "weekLabel");
            initChannelSelect("channel");
            aRecords("周报合并","/webapp/pages/channel/channel/mergeWeekList")
            var height = $(window).height();
            var nav_height = $("#nav")._outerHeight();
            var form_height = $("#ff").height();
            //电商月不拆分周
            // if (window.parent.workMode.toUpperCase() == "ONLINE_M") {
            //     $("#startPercentage").next().hide();
            //     $("#endPercentage").next().hide();
            // }

            $("#datagrid").css("max-height", (height - nav_height - form_height - 20));
            $("#datagrid").datagrid({
                rownumbers: true,
                pagination: true,
                // idField: "id",   //如果打开会出现一个异常，先单选一个渠道，点击全选，在取消全选，此时应当没有选取任何渠道，但是使用js读取选取行的时候，却有值，是最后一行，目前尚不知道什么原因，巨坑
                checkOnSelect: false,   //用户点击行的时候是否选取
                onBeforeSelect: function (index, row) {
                    window.currentRecord = row;
                    return false;
                },
            }).datagrid("getPager").pagination({
                onSelectPage: searchBy
            });
            searchBy(1, 100);
        });

        function actions(val, row) {
            return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;' onclick='disableOne(\"" + row.dataReportCode + "\")'>删除</a>";
        }

        function disableOne(code) {
            $.messager.confirm("提示", "您确定删除该合并记录吗？", function (flag) {
                if (flag) {
                    $.ajax({
                        url: config.url + "/channelMonth/delete/" + code,
                        type: 'DELETE',
                        dataType: "json",
                        xhrFields: {withCredentials: true},
                        beforeSend: function () {
                            $.messager.progress({
                                title: "请稍等",
                                interval: 50,
                                text: "操作中..."
                            });
                        },
                        complete: function () {
                            $.messager.progress("close");
                        },
                        success: function (data) {
                            $.messager.progress("close");
                            if (data.code == 200) {
                                $.messager.alert("提示", data.msg, "info", function () {
                                    searchBy();
                                });
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, thrown) {
                            $.messager.alert("提示", "操作失败", "error");
                        }
                    });
                }
            });
        }


        function searchBy(page, size) {
            var dtd = $.Deferred();
            var data = getFormData(document.getElementById("ff"));
            data['page'] = page || 1;
            data['size'] = page || 100;
            data.reportType = window.parent.workMode.toUpperCase();
            $.ajax({
                url: config.url + "/channelMonth/mergeWeekChannelList",
                type: "get",
                data: transfer(data),
                dataType: "json",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 50,
                        text: "数据加载中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (data) {
                    $.messager.progress("close");
                    if (data.code == 200) {
                        $("#datagrid").datagrid("loadData", data.msg);
                        $("#datagrid").datagrid("clearChecked");    //清除所有选择框，参考https://zhidao.baidu.com/question/1307834243364649099.html
                    }
                    dtd.resolve();
                },
                error: function (data) {
                    $.messager.progress("close");
                }
            });
            return dtd.promise();
        }

        function mergeWeek() {
            var selectedRow = $("#datagrid").datagrid("getChecked");
            if (selectedRow != null && selectedRow.length > 0) {
                var codes = selectedRow.map(function (item) {
                    return item.code;
                }).join(",");
                var year = $("#year").textbox('getValue');
                var month = $("#month").textbox('getValue');
                var reportType=window.parent.workMode.toUpperCase();
                if (reportType != "MONTH" && reportType != "ONLINE_M") {
                    $.messager.alert("提示", "目前只支持【线上月/线下月】模式", "error");
                    return;
                }
                $.messager.confirm("提示", "您确定要对所选渠道" + year + "年" + month + "月进行合并操作吗?", function (r) {
                    if (r) {
                        $.ajax({
                            url: config.url + "/channelMonth/mergeWeekData",
                            type: 'POST',
                            xhrFields: {withCredentials: true},
                            dataType: 'json',
                            data: {'channelCodes': codes, 'year': year, 'month': month, 'reportType': reportType},
                            beforeSend: function () {
                                $.messager.progress({
                                    title: "请稍等",
                                    interval: 50,
                                    text: "合并中..."
                                });
                            },
                            complete: function () {
                                $.messager.progress("close");
                            },
                            success: function (data) {
                                if (data.code == 200) {
                                    $.messager.alert("提示", "合并任务已提交", "info", function () {
                                        searchBy(1, 100);
                                    });
                                } else {
                                    $.messager.alert("提示", data.msg, "error");
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, thrown) {

                            }
                        });
                    }
                });
            } else {
                $.messager.alert("提示", "请选择相关记录", "error");
                return false;
            }
        }

        function mergeType(val,row){
            if(val=="SUCCESS"){
                return "<a class='l-btn-left l-btn-icon-left' style='width:24px;height:24px;'><span class='l-btn-icon icon-ok'></span></a>";
            }else if(val == "FAIL"){
                return "<a class='l-btn-left l-btn-icon-left' style='width:24px;height:24px;'><span class='l-btn-icon icon-no'></span></a>";
            }else if (val == "RUNNING") {
                return "<span style='color: #4cae4c'>合并中</span>";
            }else if (val == "WAITING") {
                return "<span style='color: goldenrod'>等待合并</span>";
            }else {
                return val;
            }
        }

        function dateAction(val,row){
            if (val == true) {
                return "是";
            }
            return "否";
        }
        function weekAction(val,row){
            if (!val) {
                return "-";
            }
            let match = val.match(/\(([^)]+)\)/);
            if (!match) {
                return val;
            }
            return match[1] == 0 ? ("<span style='color: red'>" + val + "</span>") : val;
        }
    </script>
</html>